package test

import config.MyFile

import scala.collection.mutable

object test7 extends MyFile {


  def main(args: Array[String]): Unit = {

    val scores = Array(1, 3, 5, 6, 8)
    val ages = Array(1, 2, 7, 5, 8)
   println( bestTeamScore(scores, ages))

  }

  def bestTeamScore(scores: Array[Int], ages: Array[Int]): Int = {
    val aIndex = ages.zipWithIndex
    val agesReverse = aIndex.sortBy(_._1).reverse
    agesReverse.zipWithIndex.map { case (x, i) =>
      var sum = scores(x._2)
      var max = scores(x._2)
      agesReverse.drop(i + 1).foreach { y =>
        if ((x._1 != y._1 && max > scores(y._2)) || (x._1 == y._1)) {
          max = scores(y._2)
          sum += scores(y._2)
        }
      }
      sum
    }.max
  }


  def minOperations(nums: Array[Int], x: Int): Int = {
    if (nums.sum < x) {
      -1
    } else {
      var headX = x
      var headX2 = x

      val headMap = nums.filter { h =>
        headX = headX - h
        headX >= 0
      }.zipWithIndex.map { case (h, i) =>
        headX2 = headX2 - h
        headX2 -> (i + 1)
      }.toMap

      val headR = headMap.getOrElse(0, -1)

      var lastX = x
      var lastX2 = 0

      val lastMap = nums.reverse.filter { l =>
        lastX = lastX - l
        lastX >= 0
      }.zipWithIndex.map { case (l, i) =>
        lastX2 += l
        lastX2 -> (i + 1)
      }.toMap

      val lastR = lastMap.getOrElse(x, -1)

      val allR = headMap.keys.toSeq.intersect(lastMap.keys.toSeq).map { a =>
        headMap(a) + lastMap(a)
      }

      (Seq(headR, lastR) ++ allR).filter(_ != -1).minOption.getOrElse(-1)
    }
  }

  def kWeakestRows(mat: Array[Array[Int]], k: Int): Array[Int] = {
    mat.map(_.sum).zipWithIndex.groupBy(_._1).toArray.sortBy(_._1).flatMap { x =>
      x._2.sortBy(_._2).map(_._2)
    }.take(k)

  }

  def countValidWords(sentence: String): Int = {
    val split1 = sentence.split(" ").map(_.trim).filter(_ != "")
    val num = (0 to 9).map(x => x.toString)
    val punc = Seq("!", ".", ",")
    split1.count { x =>
      x.count(y => num.indexOf(y.toString) != -1) == 0 && {
        if (x.exists(y => punc.contains(y.toString))) {
          x.count(y => punc.contains(y.toString)) == 1 && punc.contains(x.last.toString)
        } else {
          true
        }
      } && {
        if (x.indexOf("-") != -1) {
          !x.startsWith("-") && !x.endsWith("-") && x.count(y => y.toString == "-") == 1 && {
            val i = x.indexOf("-")
            punc.indexOf(x(i - 1).toString) == -1 && punc.indexOf(x(i + 1).toString) == -1
          }
        } else {
          true
        }
      }
    }
  }
}
